package com.yanqu.road.server.cmd.banquet.crossBanquet.auto;

import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import java.text.MessageFormat;

@Cmd(code = Protocol.S_BANQUET_CROSS_ACTIVITY_JOIN_AUTO_RESP, desc = "自动赴宴来发东西")
public class BanquetAutoJoinRespCmd extends AbstractCommand {

	private CrossBanquetProto.CrossToBanquetAutoJoinSetResp reqMsg;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		reqMsg = CrossBanquetProto.CrossToBanquetAutoJoinSetResp.parseFrom(packet.getBytes());
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

		if(!StringUtils.isNullOrEmpty(reqMsg.getConsume())){
			player.getModule(PreReduceModule.class).restorePreReduce(reqMsg.getConsume());
			if(reqMsg.getRet() == 0){
				player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(reqMsg.getConsume()), eLogMoneyType.Banquet,eLogMoneyType.BanquetCrossAutoJoinSet);
			}
		}else{//取消设置
			//目前只有type1是5的支持
			Property allReward = new Property();
			for(CrossBanquetProto.BanquetAutoJoinTmp joinTemp : reqMsg.getJoinsList()){
				if(!BanquetConfigMgr.isBanquetConsumeContain(5,joinTemp.getConsume())){
					continue;
				}
				Property giftConsume = PropertyHelper.parseStringToProperty(joinTemp.getConsume());
				giftConsume.goodCountMultiNum(joinTemp.getNum());
				allReward.addProperty(giftConsume);
			}
			UserInfo userInfo = UserMgr.getUserInfo(reqMsg.getUserId());
			if(reqMsg.getType() == 4){
                LogMgr.addLogActivityReward(reqMsg.getUserId(), reqMsg.getAcId(), eActivityType.CrossRankActivity.getValue(), 5,
                        -1, 0, false, PropertyHelper.parsePropertyToString(allReward));
            }
			if(userInfo == null){
				log.error("没有找到玩家数据，自动回宴没有返还{},积分{}",allReward,reqMsg.getIntegral());
				return;
			}
			if(!StringUtils.isNullOrEmpty(PropertyHelper.parsePropertyToString(allReward))){
				if(reqMsg.getType() == 1){
					String mailTitle = MultipleLanguageMgr.getContent(MailManager.MALL_BANQUET_AUTO_JOIN_CANCEL_TITLE1, userInfo.getLanguage());
					String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MALL_BANQUET_AUTO_JOIN_CANCEL_CONTENT1, userInfo.getLanguage())
							,reqMsg.getJoinTimes1());
					MailManager.sendMail(userInfo.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(allReward), mailContent, mailTitle);
				}else if(reqMsg.getType() == 2){
					String mailTitle = MultipleLanguageMgr.getContent(MailManager.MALL_BANQUET_AUTO_JOIN_CANCEL_TITLE2, userInfo.getLanguage());
					String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MALL_BANQUET_AUTO_JOIN_CANCEL_CONTENT2, userInfo.getLanguage())
							,reqMsg.getJoinTimes2());
					MailManager.sendMail(userInfo.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(allReward), mailContent, mailTitle);
				}else if(reqMsg.getType() == 3){
					String mailTitle = MultipleLanguageMgr.getContent(MailManager.MALL_BANQUET_AUTO_JOIN_CANCEL_TITLE3, userInfo.getLanguage());
					String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MALL_BANQUET_AUTO_JOIN_CANCEL_CONTENT3, userInfo.getLanguage())
							,reqMsg.getJoinTimes3());
					MailManager.sendMail(userInfo.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(allReward), mailContent, mailTitle);
				}else if(reqMsg.getType() == 4){
					String mailTitle = MultipleLanguageMgr.getContent(MailManager.MALL_BANQUET_AUTO_JOIN_CANCEL_TITLE, userInfo.getLanguage());
					String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MALL_BANQUET_AUTO_JOIN_CANCEL_CONTENT, userInfo.getLanguage())
							,reqMsg.getJoinTimes1(),reqMsg.getJoinTimes2(),reqMsg.getJoinTimes3());
					MailManager.sendMail(userInfo.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(allReward), mailContent, mailTitle);
				}
			}
			if(reqMsg.getIntegral() != 0){
				String mailTitle = MultipleLanguageMgr.getContent(MailManager.MALL_BANQUET_AUTO_JOIN_INTEGRAL_TITLE, userInfo.getLanguage());
				String mailContent = MultipleLanguageMgr.getContent(MailManager.MALL_BANQUET_AUTO_JOIN_INTEGRAL_CONTENT, userInfo.getLanguage());
				String integralReward = GameConfig.BANQUET_INTEGRAL + "=" + reqMsg.getIntegral();
				MailManager.sendMail(userInfo.getUserId(), eMailType.SYSTEM.getValue(), integralReward, mailContent, mailTitle);
			}
		}

		if(player != null){
			CrossBanquetProto.BanquetAutoJoinSetResp.Builder respMsg = CrossBanquetProto.BanquetAutoJoinSetResp.newBuilder();
			respMsg.setRet(reqMsg.getRet());
			player.sendPacket(Protocol.U_BANQUET_CROSS_ACTIVITY_JOIN_AUTO_SET, respMsg);
		}
	}

	@Override
	public void sendData(GamePlayer player) {

	}
}
